import { defineStore } from 'pinia'
import { ref } from 'vue'
import { request } from '@/utils/request'

export interface Equipment {
  id: number
  name: string
  brand: string
  model: string
  categoryId: number
  description: string
  specifications: string
  dailyRentPrice: number
  status: 'AVAILABLE' | 'RENTED' | 'MAINTENANCE' | 'DAMAGED'
  imageUrl?: string
  purchaseDate: string
  purchasePrice: number
  createdAt: string
  updatedAt: string
}

export interface EquipmentStatistics {
  total: number
  available: number
  rented: number
  maintenance: number
}

export const useEquipmentStore = defineStore('equipment', () => {
  const equipmentList = ref<Equipment[]>([])
  const statistics = ref<EquipmentStatistics>({
    total: 0,
    available: 0,
    rented: 0,
    maintenance: 0
  })
  const loading = ref(false)

  const fetchEquipmentList = async () => {
    loading.value = true
    try {
      const data = await request.get('/equipment')
      equipmentList.value = data
    } catch (error) {
      console.error('获取设备列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const fetchStatistics = async () => {
    try {
      const data = await request.get('/equipment/statistics')
      statistics.value = data
    } catch (error) {
      console.error('获取统计数据失败:', error)
      throw error
    }
  }

  const getAvailableEquipment = async () => {
    loading.value = true
    try {
      const data = await request.get('/equipment/available')
      return data
    } catch (error) {
      console.error('获取可用设备失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const saveEquipment = async (equipment: Partial<Equipment>) => {
    loading.value = true
    try {
      let data
      if (equipment.id) {
        data = await request.put(`/equipment/${equipment.id}`, equipment)
      } else {
        data = await request.post('/equipment', equipment)
      }
      await fetchEquipmentList()
      return data
    } catch (error) {
      console.error('保存设备失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const deleteEquipment = async (id: number) => {
    loading.value = true
    try {
      await request.delete(`/equipment/${id}`)
      await fetchEquipmentList()
    } catch (error) {
      console.error('删除设备失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  const updateEquipmentStatus = async (id: number, status: Equipment['status']) => {
    loading.value = true
    try {
      const data = await request.put(`/equipment/${id}/status`, { status })
      await fetchEquipmentList()
      return data
    } catch (error) {
      console.error('更新设备状态失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  return {
    equipmentList,
    statistics,
    loading,
    fetchEquipmentList,
    fetchStatistics,
    getAvailableEquipment,
    saveEquipment,
    deleteEquipment,
    updateEquipmentStatus
  }
})